﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Reflection;

namespace Crystal.CLL
{
    class ValidationMessageManager
    {
        #region Member Variable
        private static Dictionary<string, string> _currentMessage = null;
        #endregion

        #region Constructors
        static ValidationMessageManager()
        {
            LoadMessagesFromXml();
        }

        #endregion

        #region Methods

        /// <summary>
        /// This Function is used to Reload Message From XML File it Check that Hashtable object Current is null
        /// then it Reload message from Xml Files
        /// </summary>
        public static void ReLoadMessagesFromXml()
        {
            _currentMessage = null;
            LoadMessagesFromXml();
        }

        /// <summary>
        /// This Function is used to load Message From XML File it Check that Hashtable object Current is null
        /// then it Load message from Xml Files Otherwise it it doesnot Load message
        /// </summary>
        private static void LoadMessagesFromXml()
        {
            //CommonCLL common = new CommonCLL();
            if (_currentMessage == null)
            {
                DataSet dsMessages = new DataSet();
                Assembly _assembly;
                _assembly = Assembly.GetExecutingAssembly();
                dsMessages.ReadXml(_assembly.GetManifestResourceStream("Crystal.CLL.Components.EntityFiles.ErrorMessage.xml"));
                _currentMessage = new Dictionary<string, string>();

                if (dsMessages.Tables.Count != 0)
                {
                    DataTable dtMessages = dsMessages.Tables[0];
                    if (dtMessages.Rows.Count != 0)
                    {
                        foreach (DataRow drMessage in dtMessages.Rows)
                            _currentMessage.Add(Convert.ToString(drMessage[0]), Convert.ToString(drMessage[1]));
                    }
                    dtMessages = null;
                }
                dsMessages = null;
            }
        }

        /// <summary>
        /// Get The Message from Message Hashtable and format it with the argumaents passed.
        /// </summary>
        /// <param name="strMessageName">string</param>
        /// <param name="args">param object[]</param>       
        /// <returns>string</returns>
        /// 
        public static string GetMessage(string messageText)
        {
            return (_currentMessage.ContainsKey(messageText) == true ? _currentMessage[messageText].ToString() : "");
        }

        ///// <summary>
        ///// Get The Message from Message Hashtable and format it with the argumaents passed.
        ///// </summary>
        ///// <param name="strMessageName">string</param>
        ///// <param name="args">param object[]</param>
        ///// <returns>string</returns>

        #endregion
    }
}
